##rm(list = ls()) 

# # Set the working directory to the location of the current script
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))  # Set working directory to the current script's directory
getwd()  # Check the current working directory

# load in file when it isnt in the environment already
load("HurricaneDataFinal2012to2014.RData")
load("HurricaneDataFinal.RData")

#=============================================#
# TREATMENT COUNTY RELIEF POLICY 2012 to 2014 #  
#=============================================#
# # select only the needed variables

dta_ReliefSurrounding <- HurricaneDataPlacebo
dta_ReliefSurrounding <- HurricaneDataPlacebo[, c("gender2", "Relief", "AgeCat2014", "Dem", "NPA", 
                                                  "Third", "Black", "Hispanic", "Other", "EIPOnly2014", "EIPOnly2012",
                                                  "VBMOnly2012", "VBMOnly2014", "EDOnly2012", "EDOnly2014", "AnyEarlyMethod2012", "AnyEarlyMethod2014",
                                                  "Voted2012", "Voted2014", "CountyCode.x")]


# subset Relief Policy (Relief) (8 counties that received relief policies); control counties just the surrounding counties)

#============================================#
#++++++++++++++++++++++++++++++++++++++++++++#


#subsetting for 12 COUNTIES, only 8 Relief as treatment, and Limited to only 4 CONTROL counties surrounding

dta_ReliefSurrounding <- subset(dta_ReliefSurrounding, dta_ReliefSurrounding$CountyCode.x=="BAY" | dta_ReliefSurrounding$CountyCode.x=="FRA" | dta_ReliefSurrounding$CountyCode.x=="GUL" 
                                | dta_ReliefSurrounding$CountyCode.x=="JAC" | dta_ReliefSurrounding$CountyCode.x=="HOL" | dta_ReliefSurrounding$CountyCode.x=="WAK" 
                                | dta_ReliefSurrounding$CountyCode.x=="WAS" | dta_ReliefSurrounding$CountyCode.x=="CAL" | dta_ReliefSurrounding$CountyCode.x=="GAD"  
                                | dta_ReliefSurrounding$CountyCode.x=="LEO" | dta_ReliefSurrounding$CountyCode.x=="LIB" | dta_ReliefSurrounding$CountyCode.x=="WAL")


### OVERALL TURNOUT ###

#Create tables to calculate overall turnout for treatment/control groups in 2012
Relief2012 = table(dta_ReliefSurrounding$Relief, dta_ReliefSurrounding$Voted2012)
Relief2014 = table(dta_ReliefSurrounding$Relief, dta_ReliefSurrounding$Voted2014)

#Calculate overall turnout among those in the control group in 2012
TurnoutControl2012 = Relief2012[1,2]/(Relief2012[1,1]+Relief2012[1,2])
#Calculate overall turnout among those in the treatment group in 2012
TurnoutTreatment2012 = Relief2012[2,2]/(Relief2012[2,1]+Relief2012[2,2])

#Calculate overall turnout among those in the control group in 2014
TurnoutControl2014 = Relief2014[1,2]/(Relief2014[1,1]+Relief2014[1,2])
#Calculate overall turnout among those in the treatment group in 2014
TurnoutTreatment2014 = Relief2014[2,2]/(Relief2014[2,1]+Relief2014[2,2])


#=============================================#
# TREATMENT COUNTY RELIEF POLICY 2016 to 2018 #  
#=============================================#


dta_ReliefSurrounding <- HurricaneData
dta_ReliefSurrounding <- HurricaneData[, c("gender2", "Relief", "AgeCat2018", "Dem", "NPA", 
                                           "Third", "Black", "Hispanic", "Other", "EIPOnly2018", "EIPOnly2016",
                                           "VBMOnly2016", "VBMOnly2018", "EDOnly2016", "EDOnly2018", "AnyEarlyMethod2016", "AnyEarlyMethod2018",
                                           "Voted2016", "Voted2018", "CountyCode.x")]


#subsetting for 12 COUNTIES, only 8 Relief as treatment, and Limited to only 4 CONTROL counties surrounding

dta_ReliefSurrounding <- subset(dta_ReliefSurrounding, dta_ReliefSurrounding$CountyCode.x=="BAY" | dta_ReliefSurrounding$CountyCode.x=="FRA" | dta_ReliefSurrounding$CountyCode.x=="GUL" 
                                | dta_ReliefSurrounding$CountyCode.x=="JAC" | dta_ReliefSurrounding$CountyCode.x=="HOL" | dta_ReliefSurrounding$CountyCode.x=="WAK" 
                                | dta_ReliefSurrounding$CountyCode.x=="WAS" | dta_ReliefSurrounding$CountyCode.x=="CAL" | dta_ReliefSurrounding$CountyCode.x=="GAD"  
                                | dta_ReliefSurrounding$CountyCode.x=="LEO" | dta_ReliefSurrounding$CountyCode.x=="LIB" | dta_ReliefSurrounding$CountyCode.x=="WAL")

### OVERALL TURNOUT ###

#Create tables to calculate overall turnout for treatment/control groups in 2016
Relief2016 = table(dta_ReliefSurrounding$Relief, dta_ReliefSurrounding$Voted2016)
Relief2018 = table(dta_ReliefSurrounding$Relief, dta_ReliefSurrounding$Voted2018)

#Calculate overall turnout among those in the control group in 2016
TurnoutControl2016 = Relief2016[1,2]/(Relief2016[1,1]+Relief2016[1,2])
#Calculate overall turnout among those in the treatment group in 2016
TurnoutTreatment2016 = Relief2016[2,2]/(Relief2016[2,1]+Relief2016[2,2])

#Calculate overall turnout among those in the control group in 2018
TurnoutControl2018 = Relief2018[1,2]/(Relief2018[1,1]+Relief2018[1,2])
#Calculate overall turnout among those in the treatment group in 2018
TurnoutTreatment2018 = Relief2018[2,2]/(Relief2018[2,1]+Relief2018[2,2])

Year = c(2012, 2014, 2016, 2018)
Turnout = c(TurnoutTreatment2012, TurnoutTreatment2014, TurnoutTreatment2016, TurnoutTreatment2018,
            TurnoutControl2012, TurnoutControl2014, TurnoutControl2016, TurnoutControl2018)
Treatment = c("Treatment", "Treatment", "Treatment", "Treatment", "Control", "Control", "Control", "Control")

parallel = data.frame(Year, Turnout, Treatment)

library(ggplot2)

parallel_graph = ggplot(parallel, aes(x = Year, y = Turnout, group = Treatment)) +
  geom_line(aes(linetype=Treatment)) +
  geom_vline(xintercept = 2016) +
  theme(text = element_text(size=24))

setwd("~/Dropbox (UFL)/Hurricane_PRQ/Code/Graphics")
ggsave("ParallelTrendGraph.png", height=10, width=15, units='in', dpi=1000)
